//
//  UserMessageViewController.swift
//  worldexchange
//
//  Created by xionghao on 2019/2/20.
//  Copyright © 2019 xionghao. All rights reserved.
//

import UIKit
import MJRefresh

class UserMessageViewController: QMUICommonTableViewController {
    
    
    var messages:[OrderItem] = [OrderItem]()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = LanguageHelper.getString(key: "短消息")
        setupView()
        // Do any additional setup after loading the view.
    }
    
    
    
    
    func setupView() {
//        self.tableView.qmui_cel
        self.tableView.qmui_cacheCellHeightByKeyAutomatically = true
        
        self.tableView.rowHeight = UITableView.automaticDimension
        self.tableView.estimatedRowHeight = 50
        self.tableView.register(UINib(nibName: "MessageViewCell", bundle: nil), forCellReuseIdentifier: "message-cell")
        self.tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.loadData()
        })
        self.tableView.mj_header.beginRefreshing()
    }
    
    func loadData() {
    
        if  let token = userData?.token {
             provider.rx.request(WE.message(token: token)).mapString()
                .subscribe(onSuccess: { (result) in
                    
                    if let messages = [OrderItem].deserialize(from: result) as? [OrderItem] {
                        self.messages.removeAll()
                        self.messages.append(contentsOf: messages)
                        self.tableView.reloadData()
                    }
                    
                     self.tableView.mj_header.endRefreshing()
                }) { (error) in
                    print(error)
                    self.tableView.mj_header.endRefreshing()
            }
        }
       
    }
    
    override func qmui_tableView(_ tableView: UITableView, cacheKeyForRowAt indexPath: IndexPath) -> NSCopying {
       let item = messages[indexPath.row]
        return "message-cell-\(item.id)" as NSCopying
    }
    
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return messages.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "message-cell") as! MessageViewCell
        let locale = LanguageHelper.shareInstance.locale
        if locale == "en" {
            cell.message.text = messages[indexPath.row].e_content
        } else if (locale == "th") {
             cell.message.text = messages[indexPath.row].t_content
        }else {
             cell.message.text = messages[indexPath.row].content
        }
       
        return cell
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}
